/**
 * All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
 * BaseFlowSkipService
 * 流程跳过
 * 修改纪录
 * 2021-02-23 版本：1.0 zhanglongfei 创建。
 * 
 * @version 版本：1.0
 * @author 作者：zhanglongfei
 * 创建日期：2021-02-23
 */
package com.topscomm.base.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.topscomm.base.BaseSystemConst;
import com.topscomm.base.pojo.BaseFlowCheckItemEntity;
import com.topscomm.base.pojo.BaseFlowSkipEntity;
import com.topscomm.base.pojo.BaseFlowSkipInsEntity;
import com.topscomm.base.service.IBaseFlowSkipInsService;
import com.topscomm.base.service.IBaseFlowSkipService;
import com.topscomm.base.service.impl.auto.BaseFlowSkipServiceAuto;
import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.util.PrimaryKeyUtil;
import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.cbo.service.ICboApprovalNodeInsService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;

@Service(value = "baseFlowSkipService")
@Scope(value = "singleton")
public class BaseFlowSkipService extends BaseFlowSkipServiceAuto implements IBaseFlowSkipService {
	@Autowired
	IBaseFlowSkipInsService baseFlowSkipInsService;
	@Autowired
	IBaseFlowSkipService baseFlowSkipService;
	@Autowired
	ICboApprovalNodeInsService cboApprovalNodeInsService;

	@Override
	protected void beforeInsert(BasicEntity entity) {
		super.beforeInsert(entity);
		if (entity.getId() <= 0) {// 赋值Id和单号
			long[] arrayId = PrimaryKeyUtil.GeneIDs(entity.getTableName(), 1);
			entity.setId(arrayId[0]);
			String code = String.valueOf(arrayId[0]);
			code = BaseSystemConst.BASE_FLOW_SKIP_CODE_PREFIX + code.substring(0, 6) + code.substring(9);
			entity.setValue(BaseFlowCheckItemEntity.FieldCode, code);
		}
	}

	/**
	 * @description:保存数据
	 * @param paraMap
	 * @author: zhanglongfei
	 * @date: 2021年2月26日下午5:45:29
	 * @modify:
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveData(Map<String, Object> paraMap) {
		long flowId = ConvertUtil.convertToLong(paraMap.get("flowid"));
		String sourceType = ConvertUtil.convertToString(paraMap.get("sourcetype"));
		String sourceId = ConvertUtil.convertToString(paraMap.get("sourceid"));
		String codeListStr = ConvertUtil.convertToString(paraMap.get("codelist"));
		baseFlowSkipInsService.deleteByWhere("flowid='" + flowId + "' and sourcetype='" + sourceType + "' and sourceid='" + sourceId + "'");
		if (StringUtil.isEmpty(codeListStr)) {
			return;
		}
		String[] codeList = codeListStr.split(",");
		List<BasicEntity> insertList = new ArrayList<>();
		for (String code : codeList) {
			BaseFlowSkipInsEntity entity = new BaseFlowSkipInsEntity();
			entity.setFlowid(flowId);
			entity.setSkipcode(code);
			entity.setSourcetype(sourceType);
			entity.setSourceid(sourceId);
			insertList.add(entity);
		}
		baseFlowSkipInsService.insertBatch(insertList);
	}

	/**
	 * @description:判断某个节点是否可以允许跳过
	 * @param id
	 * @return
	 * @author: zhanglongfei
	 * @date: 2021年3月1日下午5:59:35
	 * @modify:
	 */
	@Override
	public boolean nodeCouldSkip(String nodeInsId) {
		CboApprovalNodeInsEntity nodeIns = cboApprovalNodeInsService.queryById(nodeInsId);
		StringBufferProxy where = new StringBufferProxy();
		where.appendLineSingle("flowid='{0}' and code in (select skipcode from BaseFlowSkipIns where sourcetype='{1}' and sourceid='{2}') and enabled=1", nodeIns.getFlowid(), nodeIns
				.getSourcetype(), nodeIns.getSourceid());
		List<BaseFlowSkipEntity> skipList = baseFlowSkipService.queryByWhere(where.toString());
		return skipList.stream().anyMatch(m -> !StringUtil.isEmpty(m.getNodeids()) && m.getNodeids().indexOf(nodeIns.getNodeid() + "") > -1);
	}
}
